package com.search.controller;

import com.alibaba.fastjson2.JSONObject;
import com.search.dao.BorrowBookDao;
import com.search.entity.BorrowBook;
import com.search.entity.CanBorrowBookVo;
import com.search.service.Add2Service;
import com.search.service.BorrowBookService;
import com.search.service.DoubanBookAllLibsService;
import com.search.service.DoubanBookCapitalService;
import com.search.service.impl.DoubanListFromCapitalService;
import com.search.service.impl.DoubanTagCapitalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class BorrowBookController {


    @Autowired
    private BorrowBookDao borrowBookDao;

    @Autowired
    private Add2Service add2Service;

    @Autowired
    private DoubanBookCapitalService doubanBookCapitalService;

    @Autowired
    private DoubanBookAllLibsService doubanBookAllLibsService;

    @Autowired
    private BorrowBookService borrowBookService;

    @RequestMapping("/queryBooksFromCapital")
    public JSONObject queryBooksFromCapital(String url,String classify,String source){

        JSONObject result = new JSONObject();

        /*if(url.contains("doulist")){
            //result =doubanListFromCapitalService.queryBooks(url,source,classify);
            doubanBookCapitalService = new DoubanListFromCapitalService();
        }else if(url.contains("tag")){
            //result =doubanTagCapitalService.queryBooks(url,source,classify);
            doubanBookCapitalService = new DoubanTagCapitalService();
        }*/
        doubanBookCapitalService.queryBooks(url,source,classify);

        return result;
    }

    @RequestMapping("/queryBooksFromLibs")
    public JSONObject queryBooksFromLibs(String url,String classify,String source){

        JSONObject result = new JSONObject();

        /*if(url.contains("doulist")){
            //result =doubanListFromCapitalService.queryBooks(url,source,classify);
            doubanBookCapitalService = new DoubanListFromCapitalService();
        }else if(url.contains("tag")){
            //result =doubanTagCapitalService.queryBooks(url,source,classify);
            doubanBookCapitalService = new DoubanTagCapitalService();
        }*/
        doubanBookAllLibsService.queryBooks(url,source,classify);

        return result;
    }

    @RequestMapping("borrowBooksPage")
    public JSONObject borrowBooksPage(BorrowBook book){

        JSONObject res = new JSONObject();
        book.setLibrary("capital");
        List<BorrowBook> list = borrowBookService.find(book);
        /*for(BorrowBook borrowBook:list){
            borrowBook.setIsbn(borrowBook.getIsbn().trim());
        }*/
        res.put("data",list);

        int total = borrowBookService.count(book);
        res.put("total",total);

        return res;
    }

    @RequestMapping("/custom")
    public JSONObject custom(@RequestParam int begin, @RequestParam int offset){

        JSONObject res = new JSONObject();
        List<BorrowBook> list = new ArrayList<>();
        try {
            // 加载 MySQL JDBC 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            Connection connection = DriverManager.getConnection("jdbc:mysql://124.70.55.5:3306/jeecg-boot?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&autoReconnect=true&useSSL=false", "root", "liu244584633");

            // 创建 SQL 查询语句
            String sql = "select b.book_name,a.isbn,b.subtitle,b.url,a.is_collect,a.library,a.rating,holding,b.tag,a.evaluate_num from borrow_book a left join douban_book b on a.isbn = b.isbn WHERE a.holding in (1,-1) and b.tag = '商业' and a.library = 'capital' order by a.rating desc" + " limit "+begin + ", "+offset ;
            PreparedStatement statement = connection.prepareStatement(sql);

            // 执行查询
            ResultSet resultSet = statement.executeQuery();

            // 处理结果集，将数据封装到实体类并添加到列表中
            while (resultSet.next()) {
                String bookName = resultSet.getString("book_name");
                String subtitle = resultSet.getString("subtitle");
                float rating = resultSet.getFloat("rating");
                String classify = resultSet.getString("tag");
                String url = resultSet.getString("url");
                String isbn = resultSet.getString("isbn");
                int evaluate_num = resultSet.getInt("evaluate_num");
                int isCollect = resultSet.getInt("is_collect");
                String library = resultSet.getString("library");

                list.add(new BorrowBook(bookName,subtitle,rating,classify,url,evaluate_num,isCollect,isbn,library));
            }

            res.put("data",list);

            res.put("total",434);

            // 关闭资源
            resultSet.close();
            statement.close();
            connection.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    @RequestMapping("myCollectBooksPage")
    public JSONObject myCollectBooksPage(BorrowBook book){

        JSONObject res = new JSONObject();
        List<BorrowBook> list = borrowBookService.find(book);
        /*for(BorrowBook borrowBook:list){
            borrowBook.setIsbn(borrowBook.getIsbn().trim());
        }*/
        res.put("data",list);

        BorrowBook query = new BorrowBook();
        query.setIsCollect(1);
        int total = borrowBookService.count(query);
        res.put("total",total);

        return res;
    }

    @RequestMapping("/collect")
    public int collect(BorrowBook book){
        return borrowBookService.updateNotNullById(book);
    }


}
